/**
 * 1961. 检查字符串是否为数组前缀
 * https://leetcode-cn.com/problems/check-if-string-is-a-prefix-of-array/
 */
public class Solutions_1961 {
    public static void main(String[] args) {
        String s = "iloveleetcode";
        String[] words = {"i", "love", "leetcode", "apples"};  // output: true

//        String s = "iloveleetcode";
//        String[] words = {"apples", "i", "love", "leetcode"};  // output: false

        boolean result = isPrefixString(s, words);
        System.out.println(result);
    }

    public static boolean isPrefixString(String s, String[] words) {
        StringBuilder sb = new StringBuilder();
        for (String word : words) {
            // 拼接数组前缀
            sb.append(word);
            if (s.equals(sb.toString())) {
                // 字符串为数组前缀
                return true;
            }
            if (sb.length() > s.length()) {
                // sb 长度大于 s，循环提前结束
                return false;
            }
        }
        return false;
    }
}
